﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;



namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sKey = textBox1.Text.Trim();
            if (sKey.Length == 0)
            {
                MessageBox.Show("空空如也");
                return;
            }
            if (sKey.Length <= 89 * 3)
            {
                MessageBox.Show("不够89个呀");
                return;
            }
            //textBox2.AppendText(sKey);//追加显示
            //textBox2.Text=(sKey);//设置文本显示
            string[] sArray = sKey.Split(' ');//用空格分离成一个一个字符串
            string Outstr = null;
            for (int i = 0; i < sArray.Length; i++)
            {
                Outstr += ("0X" + sArray[i] + ",");//FF变成0XFF,如此拼接 
            }
            Outstr.Substring(0, Outstr.Length - 1);//去掉最后一个，
            textBox2.Text = Outstr;
            richTextBox1.Text = sArray.Length.ToString();//显示数据个数


            String expin = "小区编号：";
            expin += sArray[0] + " " + sArray[1] + " " + sArray[2] + " " + sArray[3] + " " + sArray[4] + "\n";
            expin += "楼栋号：";
            expin += sArray[5] + " " + sArray[6] + "\n";
            expin += "单元号：";
            expin += sArray[7] + "\n";
            expin += "房间号：";
            expin += sArray[8] + " " + sArray[9] + "\n";
            expin += "姓名：";
            expin += sArray[10] + " " + sArray[11] + " " + sArray[12] + " " + sArray[13] + " " + sArray[14] + " " + sArray[15] + "\n\n";
            expin += "设置位：";
            expin += sArray[16] + "\n";
            expin += "发卡开始时间：";
            expin += sArray[17] + " " + sArray[18] + " " + sArray[19] + "\n";
            expin += "发卡截止时间：";
            expin += sArray[20] + " " + sArray[21] + " " + sArray[22] + "\n";
            expin += "CRC8：";
            expin += sArray[23] + "\n";
            expin += "卡号：";
            expin += sArray[24] + " " + sArray[25] + " " + sArray[26] + " " + sArray[27] + "\n";
            expin += "版本：";
            expin += sArray[28] + "\n";
            expin += "卡类型：";
            expin += sArray[29] + "\n";
            expin += "CRC16：";
            expin += sArray[30] + " " + sArray[31] + "\n\n";
            expin += "身份证：";
            expin += sArray[32] + sArray[33] + sArray[34] + sArray[35] + sArray[36] + sArray[37] + sArray[38] + sArray[39] + sArray[40] + "\n\n";


            expin += "小区编号(1)：";
            expin += sArray[41] + " " + sArray[42] + " " + sArray[43] + " " + sArray[44] + " " + sArray[45] + "\n";
            expin += "楼栋号(1)：";
            expin += sArray[46] + " " + sArray[47] + "\n";
            expin += "单元号(1)：";
            expin += sArray[48] + "\n";
            expin += "小区编号(2)：";
            expin += sArray[49] + " " + sArray[50] + " " + sArray[51] + " " + sArray[52] + " " + sArray[53] + "\n";
            expin += "楼栋号(2)：";
            expin += sArray[54] + " " + sArray[55] + "\n";
            expin += "单元号(2)：";
            expin += sArray[56] + "\n";
            expin += "小区编号(3)：";
            expin += sArray[57] + " " + sArray[58] + " " + sArray[59] + " " + sArray[60] + " " + sArray[61] + "\n";
            expin += "楼栋号(3)：";
            expin += sArray[62] + " " + sArray[63] + "\n";
            expin += "单元号(3)：";
            expin += sArray[64] + "\n";
            expin += "小区编号(4)：";
            expin += sArray[65] + " " + sArray[66] + " " + sArray[67] + " " + sArray[68] + " " + sArray[69] + "\n";
            expin += "楼栋号(4)：";
            expin += sArray[70] + " " + sArray[71] + "\n";
            expin += "单元号(4)：";
            expin += sArray[72] + "\n";
            expin += "房间号(1)：";
            expin += sArray[73] + " " + sArray[74] + "\n";
            expin += "房间号(2)：";
            expin += sArray[75] + " " + sArray[76] + "\n";
            expin += "房间号(3)：";
            expin += sArray[77] + " " + sArray[78] + "\n";
            expin += "房间号(4)：";
            expin += sArray[79] + " " + sArray[80] + "\n";
            expin += "CRC：";
            expin += sArray[81] + "\n";
            expin += "TEMP：";
            expin += sArray[82] + "\n";
            expin += "开始日期：";
            expin += sArray[83] + " " + sArray[84] + " " + sArray[85] + " " + sArray[86] + "\n";
            expin += "CRC：";
            expin += sArray[87] + " " + sArray[88] + "\n";

            richTextBox2.Text = expin;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // System.IO.File.WriteAllText(@"C:\Users\Gong Jin Hua\Desktop\TEM\ggg.txt", richTextBox2.Text);
            /*1杰杰的代码：直接在指定路径下建立文件 写入内容 如果以前有 就直接覆盖*/


            /*2https://wenda.so.com/q/1486411527494579?src=180*/
            // 利用SaveFileDialog，让用户指定文件的路径名
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.Filter = "文本文件|*.txt";
            if (saveDlg.ShowDialog() == DialogResult.OK)
            {
                // 创建文件，将textBox1中的内容保存到文件中
                // saveDlg.FileName 是用户指定的文件路径
                FileStream fs = File.Open(saveDlg.FileName,/*最前面using System.IO;*/
                    FileMode.Create,
                    FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);

                // 保存textBox1中所有内容（所有行）
                foreach (string line in richTextBox2.Lines)
                {
                    sw.WriteLine(line);
                }
                //关闭文件
                sw.Flush();
                sw.Close();
                fs.Close();
                // 提示用户：文件保存的位置和文件名
                MessageBox.Show("文件已成功保存到" + saveDlg.FileName);
            }

        }
    }
}